/*
 * NOTICE: THIS SOFTWARE IS THE PROPERTY OF AND CONTAINS CONFIDENTIAL INFORMATION OF POWER SHARE AND/OR ITS AFFILIATES
 * OR SUBSIDIARIES AND SHALL NOT BE DISCLOSED WITHOUT PRIOR WRITTEN PERMISSION. LICENSED CUSTOMERS MAY COPY AND ADAPT
 * THIS SOFTWARE FOR THEIR OWN USE IN ACCORDANCE WITH THE TERMS OF THEIR SOFTWARE LICENSE AGREEMENT. ALL OTHER RIGHTS
 * RESERVED.
 *
 * COPYRIGHT © 2022 FC. ALL RIGHTS RESERVED. THE WORD AND DESIGN MARKS SET FORTH HEREIN ARE TRADEMARKS AND/OR
 * REGISTERED TRADEMARKS OF POWER SHARE AND/OR ITS AFFILIATES AND SUBSIDIARIES. ALL RIGHTS RESERVED. ALL OTHER
 * TRADEMARKS LISTED HEREIN ARE THE PROPERTY OF THEIR RESPECTIVE OWNERS.
 *
 */

package com.ckx.base;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.dromara.hutool.core.bean.BeanUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用分页响应结果
 *
 * @author e-qingzhen.li
 * @date 2022-10-31 13:56
 */
@Setter
@Getter
@ToString
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public PageResult() {}

    public PageResult(Integer pageSize, Integer pageIndex, Integer totalPage, List<T> data, Long totalCount) {
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.totalPage = totalPage;
        this.data = data;
        this.totalCount = totalCount;
    }

    /**
     * 总条数
     */
    private Long totalCount;

    /**
     * 总页数
     */
    private Integer totalPage;

    /**
     * 第几页
     */
    private Integer pageIndex;

    /**
     * 每页条数
     */
    private Integer pageSize;

    /**
     * 每页的数据
     */
    @JsonProperty("rows")
    private List<T> data;

    @SuppressWarnings("unused")
    public void addAll(List<T> values) {
        if (data == null) {
            data = new ArrayList<>();
        }
        this.data.addAll(values);
    }

    public <E> PageResult<E> convert(Class<E> c) {
        PageResult<E> result = new PageResult<>();
        result.setPageSize(this.getPageSize());
        result.setTotalPage(this.getTotalPage());
        result.setPageIndex(this.getPageIndex());
        result.setTotalCount(this.getTotalCount());
        result.setData(BeanUtil.copyToList(this.data, c));
        return result;
    }

}
